<?php
/**
 *  Copyright (c) 2013, dsphinx@plug.gr
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. All advertising materials mentioning features or use of this software
 *      must display the following acknowledgement:
 *      This product includes software developed by the dsphinx.
 *   4. Neither the name of the dsphinx nor the
 *      names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY dsphinx ''AS IS'' AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL dsphinx BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */
include('Private/admin_advert/auth_db.php');

$sql_1 = "SELECT category.id AS aid, category.name AS aname, category_matrix.category_id AS bid, category_matrix.parent_id AS bparent_id, category_matrix.ordering AS bordering FROM category JOIN category_matrix WHERE category_matrix.parent_id = 0 AND category.id = category_matrix.category_id GROUP BY category_matrix.ordering";
$result = mysqli_query($con, $sql_1);
$_categories = '<option value="0"></option>';
// echo $sql_1;
while($row = mysqli_fetch_array($result))
{
    $sql_2 = "SELECT category.id AS aid, category.name AS aname, category_matrix.category_id AS bid, category_matrix.parent_id AS bparent_id, category_matrix.ordering AS bordering FROM category JOIN category_matrix WHERE category_matrix.parent_id =".$row['aid']." AND category.id = category_matrix.category_id GROUP BY category_matrix.ordering";
    $result1 = mysqli_query($con,$sql_2);
    $_categories = $_categories . '<optgroup label="' . $row['aname'] . '">';
    /*echo $row['id'] . " " . $row['name'] . " " . $row['divname'] . " " . $row['parent_id']. " " . $row['action_category_id'];
    echo "<br>";*/
//    echo $sql_2;
    while($row1 = mysqli_fetch_array($result1))
    {
        $_categories = $_categories . '<option value="' . $row1['aid'] . '">' . $row1['aname'] . '</option>';
    }

    $_categories = $_categories . '</optgroup>';
}
/* $_categories = '  <option value="0"></option>
     <optgroup label="Κινητά">
                <option value="1"> Αυτοκίνητα</option>
                <option value="2"> Καράβια</option>
                <option value="3"> Αεροπλάνα</option>
       </optgroup>
          <optgroup label="Διάφορα">
                 <option value="4"> Φίλους</option>
                <option value="5"> Παλιούς</option>
       </optgroup>



                ';
*/


    $db = MyDB::db();

$_query_side = isset($_GET['extra']) ? TRUE : FALSE;
$_query_category = $_query1 = $_query_ok =  NULL;
$_query_id         = isset($_REQUEST['query_id']) ? intval($_REQUEST['query_id']) : NULL;



    if ($_query_id) {
        include('show.php');
        return;
    }


if ($_query_side) {

    $_query1         = isset($_REQUEST['query']) ? $_REQUEST['query'] : NULL;
    $_query_category = isset($_REQUEST['category']) ? $_REQUEST['category'] : NULL;


}


echo '<h5> Αναζήτησε την αγγελία που ψάχνεις στην κατηγορία που ανήκει ή με τον δικό σου όρο αναζήτησης </h5>';

if (!isset($_SESSION['Auth']))
    echo '<h5> Κάνε εγγραφή ή σύνδεση για να ανεβάσεις την δική σου αγγελία </h5>';
//  echo Html::javascript('jquery.livesearch.js');

?>





<div class="well well-large" style="margin: auto; max-width: 900px; ">
    <form id="search" class="form-search" method="post" action="?page=search<?php if ($_GET['pg']) echo '&pg='.$_GET['pg'];?>" accept-charset="UTF-8">
        <input type="hidden" name="searchin" value="1">
        <input type="hidden" id="category_old" name="category_old" value="<?php echo $_query_category; ?>">
        <input type="hidden" id="query_old" name="query_old" value="<?php echo $_query1; ?>">

        <div style="display: inline; float: left;">
            <select name="category" id="category" data-placeholder="όλες οι κατηγορίες" class="chzn-select-deselect"
                    style="width:235px;  float:left; ">
                <?php echo $_categories; ?>
            </select>


        </div>

        <div style="display: inline; margin-left: 4px;margin-right: 2px; ">


            <input style=" float:inherit; " type="text" class="input-xlarge search-query" id="query" name="query"
                   placeholder=" όρος αναζήτησης" autofocus value="<?php echo $_query_ok; ?>">


        </div>
        <div style="display: inline; float: right;">

            <button type="submit" class="btn btn-success btn-large" klkkclass="btn btn-primary btn-small"> Αναζήτηση <i class="icon-search icon-white"></i>
            </button>
        </div>

        <div class="clearfix"></div>

        <div id="search_extra"></div>
    </form>
    <div class="clearfix"></div>

</div>
</div>
<div class="advert"> <a href="?adhere"> Εδώ μπορεί να μπει η δική σου διαφήμιση </a>  </div>
<br />


<?php



include('search_get_params.php');

$seach = isset($params['query']) ? '<div class="alert alert-info" style="width:85%;" ><h4>   Αναζήτηση σε  ' . $params['query'] .
    '</h4> </div>
     <div id="results">
            <br/>   <img src="Media/images/loading.gif" style="margin:auto; display:block;">       <br/>
    </div> ' : NULL;


$_HTML_CODE = '<div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div id="searching_categories" class="span2">         <!--Sidebar content-->
            </div>
            <div class="span10"  style="min-height: 400px;">  '. $seach .'               <!--Body content-->

                ';

echo $_HTML_CODE;


// echo " <h4> " . count($params) . "  πεδία </h4>";

$_HTML_CODE = '   </div>    </div>   </div>';

echo $_HTML_CODE;


    $str = serialize($params);
    $strenc = urlencode($str);

/*
*   Load All file in current Directory
*/
Controller_files::_load_directory_files(__DIR__);
?>


 <script>
     $( document ).ready(function() {

         $("#results").load("App/Ajax/search_query.php", { data : '<?php echo $strenc;?>', page : '<?php echo $_GET['pg'];?>' }, function (data) {
        //       alert( "Load was performed." +data );
         });

     });

 </script>
